//
//  SDBaseViewController.m
//  SKinDiary
//
//  Created by pcyang on 16/6/26.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDBaseViewController.h"
#import "SDLoginViewController.h"
#import "SDNavigationController.h"

@interface SDBaseViewController()

@end

@implementation SDBaseViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        _isWhiteStatusBar = NO;
        _isCustomPageJumpReport = NO;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.view.backgroundColor = RGB(0xf5, 0xf5, 0xf5);
    self.view.backgroundColor = [UIColor colorWithRGB:0xf9fafc];

    [self relayoutNaviBar];
    [self setNeedsStatusBarAppearanceUpdate];

  //  [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor colorWithRGB:0x24d9c6]];
  //  [[UINavigationBar appearance] setBarTintColor:RGB(36, 217, 198)];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didAddBlankPageSubview:) name:kDidAddBlankPageSubviewNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self.view bringSubviewToFront:_navBar];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

//    if (_isWhiteStatusBar)
//    {
//        [self setStatusBarIsWhite:YES];
//    }
//    else
//    {
//        [self setStatusBarIsWhite:NO];
//    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
//    if (!_isCustomPageJumpReport)
//    {
//        [SDCriticalPathLog setPageId:[self pageIdentifier]];    //收集上报信息
//    }
//    [WDK trackPageViewBegin:[self pageIdentifier]];
}


- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
  //  [WDK trackPageViewEnd:[self pageIdentifier]];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kDidAddBlankPageSubviewNotification object:nil];
    
}

- (void)didAddBlankPageSubview:(NSNotification*)notification
{
    [self.view bringSubviewToFront:self.navBar];
}

-(NSString*) pageIdentifier
{
    return NSStringFromClass([self class]);
}

- (BOOL)prefersStatusBarHidden
{
    return NO;
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

//- (void)setStatusBarIsWhite:(BOOL)isWhite
//{
//    if (isWhite)
//    {
//        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
//    }
//    else
//    {
//        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
//    }
//}

#pragma mark -  旋转屏幕

#pragma mark -  <6.0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}

#pragma mark -  >6.0
- (BOOL)shouldAutorotate
{
    return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}


#pragma mark -- navigationBar

- (void)setTitle:(NSString *)title {
    [self.navBar setTitle:title];
}

- (BOOL)navigationBarHidden
{
    return YES;
}

- (void)relayoutNaviBar
{
    BOOL naviHidden = [self navigationBarHidden];
    if (!naviHidden && _navBar == nil)
    {
        _navBar = [[SDNavigationBar alloc] init];
        [self.view addSubview:_navBar];
    }
}

#pragma mark ---

- (void)simulationRefreshOfPullDown
{
}

////右滑手势相关
//
//- (SDPageCtlDragbackType)dragBackType
//{
//    return SDPageCtlDragbackTypeDefault;
//}

- (void)popUp:(BOOL)animated {
    
    UINavigationController * naviCtl = self.navigationController;
    
    if ( [naviCtl isKindOfClass:[UINavigationController class]] )
    {
        [naviCtl popViewControllerAnimated:animated];
    }
    else if ( (!naviCtl || ![naviCtl isKindOfClass:[UINavigationController class]]) )
    {
        UIViewController * superCtl = self.view.viewController;
        if ( [superCtl isKindOfClass:[SDBaseViewController class]] )
        {
            [(SDBaseViewController *)superCtl popUp:animated];
        }
        else
        {
            naviCtl = superCtl.navigationController;
            if ( [naviCtl isKindOfClass:[UINavigationController class]] )
            {
                [naviCtl popViewControllerAnimated:animated];
            }
        }
    }
}

- (BOOL)responseDragbackAtScreenPoint:(CGPoint)screenPoint;
{
    if (screenPoint.x>100)
    {
        return NO;
    }
    return YES;
}

@end

@implementation SDBaseViewController(login)
-(void)showLoginViewController
{
    if ( [SDHttpRequestManagement sharedHttpRequestManagement].currentNetworkStatus == kNotReachable)
    {
        [SDAlertManagement showNetworkErrorAlert:kNoNetWorkTipWord];
        return;
    }
    SDLoginViewController *loginVC = [[SDLoginViewController alloc] init];
    
    SDNavigationController * navigationVC = [[SDNavigationController alloc] initWithRootViewController:loginVC];
    [self.navigationController presentViewController:navigationVC animated:YES completion:nil];
}

@end

